package org.lex.swing.graph;

import java.util.Comparator;

/**
 * GraphModel���Կ�����һ��һά����
 * 
 * @author Daniel Liu
 */
public interface ProgressModel {
	public int getRangeCount();

	public long getMaximum();

	public void setMaximum(long max);

	public Range getRange(int index);

	public void addRange(Range r);

	public void removeRange(Range r);

	public void removeAll();

	/**
	 * convient method for add/remove Range
	 ***/
	public void addRange(long from, long to);

	public void removeRange(long from, long to);

	public void addGraphListener(ProgressListener l);

	public void removeGraphListener(ProgressListener l);

	public static class Range implements Comparable<Range> {
		public static final Comparator<Range> START_COMPARATOR;
		public static final Comparator<Range> END_COMPARATOR;
		static {
			START_COMPARATOR = new Comparator<Range>() {
				@Override
				public int compare(Range r1, Range r2) {
					return Long.valueOf(r1.start).compareTo(r2.start);
				}
			};
			END_COMPARATOR = new Comparator<Range>() {
				@Override
				public int compare(Range r1, Range r2) {
					return Long.valueOf(r1.end).compareTo(r2.end);
				}
			};
		}
		long start = 0;
		long end = 0;

		public Range(long from, long to) {
			if (from > to) {
				long temp = from;
				from = to;
				to = temp;
			}
			this.start = from;
			this.end = to;
		}

		public long start() {
			return start;
		}

		public long end() {
			return end;
		}

		boolean hasIntersection(Range r) {
			return contains(r.start) || contains(r.end);
		}

		boolean contains(long x) {
			return x >= start && x <= end;
		}

		boolean add(Range r) {
			if (false == this.hasIntersection(r))
				return false;
			else {
				start = (start >= r.start) ? start : r.start;
				end = (end >= r.end) ? end : r.end;
				return true;
			}
		}

		@Override
		public boolean equals(Object o) {
			if (null == o)
				return false;
			if (this == o)
				return true;
			if (false == o instanceof Range)
				return false;
			Range r = (Range) o;
			return (start == r.start && end == r.end);
		}

		@Override
		public int compareTo(Range o) {
			return Long.valueOf(start).compareTo(o.start);
		}

		@Override
		public String toString() {
			return "[" + start + "," + end + "]";
		}
	}
}
